<?php
require_once("OAuth.php");
class GreeAPI {
    public $user_request;
    public $oauth_conf = array();   //应用配置
    public $params_from_request = array();  //from page 
    public $params_to_request = array();  //from page 
    public $access_token;
    public $oauth_consumer;
    public $signature_method;
	
    //构造函数
    public function __construct($app_conf){
        $this->oauth_conf = $app_conf;
        $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1();
        $this->oauth_consumer = new OAuthConsumer($this->oauth_conf['consumer_key'], $this->oauth_conf['consumer_secret']);
        $this->user_request = OAuthRequest::from_request(null, null, null);
    }
    
    //设置从平台传递过来的参数
    public function setPlatParams($key,$value){
        $this->params_from_request[$key] = $value;
    }
    
    //get single param
    public function getParam($key){
        return $this->params_from_request[$key];
    }
    
    public function setRequestData($key,$value){
        $this->params_to_request[$key] = $value;
    }
    
    public function getReqParam($key){
        return $this->params_to_request[$key];
    }
    
    public function setAccessToken(){
        $this->access_token = new OAuthToken($this->getParam('oauth_token'),$this->getParam('oauth_token_secret'));
    }
    public function getAccessToken(){
        return $this->access_token;
    }
    public function signatureValid(){
        $signature_valid = $this->signature_method->check_signature($this->user_request, $this->oauth_consumer, 
    $this->getAccessToken(), $this->getParam('oauth_signature'));
        if ($signature_valid !== true) {
            header('HTTP/1.1 403 Forbidden');
        }else{
            header("Content-type: text/html; charset=UTF-8");
            echo "validation OK <br />";
            die();
        }
    }
    public function getAuthHeader($endpoint_url,$http_method){

        $oauth_request = OAuthRequest::from_consumer_and_token($this->oauth_consumer, $this->getAccessToken(), $http_method, $endpoint_url, $this->params_to_request);
        $oauth_request->sign_request($this->signature_method, $this->oauth_consumer, $this->getAccessToken());

        $authorization_header_string = $oauth_request->to_header();
        $authorization_header = substr($authorization_header_string, strlen('Authorization:'));
        if($http_method == 'POST'){
            $authorization_header .=",xoauth_requestor_id=\"".$this->getReqParam('xoauth_requestor_id').'"';
        }
		//return $authorization_header;
        return urldecode($authorization_header);
    }
    public function curlRequest($endpoint_url,$http_method,$authorization_header,$post_data = null){
        $useragent = 'TalentWalker API PHP5 Client 1.0 (curl) ' ;
        if(function_exists('curl_init')){
            try{
                $ch = curl_init();
                if($http_method == 'GET'){
					curl_setopt($ch, CURLOPT_URL, $endpoint_url . '&xoauth_requestor_id=' .$this->getReqParam('xoauth_requestor_id'));
                    curl_setopt($ch, CURLOPT_HTTPGET,true);
                }else{
                    curl_setopt($ch, CURLOPT_URL, $endpoint_url);
                    curl_setopt($ch, CURLOPT_POST,true);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
                }
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
                curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置超时时间
                curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type:application/json; charset=utf8','Authorization:'.$authorization_header));
                $response_body = curl_exec($ch);			
				 $response_body = json_decode($response_body,true);
				//var_dump($response_body);die('test');
                curl_close($ch);
            }catch(Exception $e){
                var_dump($e);
            }
            return $response_body;
        }else{
            //$response_body = file_get_contents($endpoint_url);
            //header('HTTP/1.1 500 Internal Server Error');
            //echo "failed", "\n";
            //exit();
            return false;
        }
    }
}
?>